package com.haidechizi.doublecache.process;

import com.haidechizi.doublecache.annotation.DoubleCache;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

public class AopProcessContext implements ProcessContext {

    private ProceedingJoinPoint point;

    private Object value;

    private String cacheKey;

    public AopProcessContext(ProceedingJoinPoint point) {
        this.point = point;
    }

    @Override
    public String[] getParameterNames() {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();

        //拼接解析springEl表达式的map
        return signature.getParameterNames();
    }

    @Override
    public Object[] getArgs() {
        return point.getArgs();
    }

    @Override
    public Object getValue() {
        return this.value;
    }

    @Override
    public String getCacheKey() {
        return this.cacheKey;
    }

    @Override
    public DoubleCache getDoubleCache() {
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        DoubleCache doubleCache = method.getAnnotation(DoubleCache.class);
        return doubleCache;
    }

    @Override
    public void setValue(Object value) {
        this.value = value;
    }

    @Override
    public void setCacheKey(String cacheKey) {
        this.cacheKey = cacheKey;
    }

    @Override
    public Object proceed() throws Throwable {
        return point.proceed();
    }
}
